// pages/oneself/profile/profile.js
const app = getApp()
const connector = require('../../../utils/connector.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bar_Height: wx.getSystemInfoSync().statusBarHeight + 45,
    name: "暂未填写",//姓名
    sex: "暂未填写",//性别
    phone: "暂未填写",//手机
    address: "暂未填写",//地址
    company: "暂未填写",//学校
    types: "暂未填写",//车型
    user:{
      idcard: "暂未填写",
      mail_address: "暂未填写",
      phone: "暂未填写",
      realname: "暂未填写",
      school_id: "暂未填写",
    },

    identity: app.globalData.identity,//身份：false：学员（默认）true：教练

  },
  //测试：身份转换
  identity(e) {
    this.setData({
      identity: !app.globalData.identity
    })
    app.globalData.identity = !app.globalData.identity

  },
  //编辑
  redact(e) {
    wx.navigateTo({
      url: '/pages/oneself/profile/redact/redact',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    // queryWebUser(that)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideShareMenu()
    vipUserGet(this)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {
    var that = this
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新

  },

})

//获取我的信息(废)
function queryWebUser(that) {
  wx.request({
    url: app.urls + '/xinyu/WebUser/queryWebUser',
    method: 'POST',
    header: app.token,
    success: function (res) {
      console.log(res)
      let name = res.data.data.wuName,
        phone = res.data.data.userPhone,
        sex = res.data.data.sex,
        email = res.data.data.email;
      wx.setStorageSync("myUser", res.data.data)
      if (name == '' || !name) name = "暂未填写"

      if (phone != '' && phone) phone = phone.slice(0, 3) + " **** " + phone.slice(7)
      else phone = "暂未填写"

      if (sex == 1) sex = "男"
      else if (sex == 2) sex = "女"
      else sex = "暂未填写"

      if (email == '' || !email) email = "暂未填写"
      that.setData({
        phone: phone,
        name: name,
        sex: sex,
        email: email
      })
    }
  })
}
//获取用户信息
function vipUserGet(that){
  if(wx.getStorageSync("users") && wx.getStorageSync("users").id){
    that.setData({
      user:wx.getStorageSync("users")
    })
  }else{
    connector.getFunc(connector.connector().vipUserGet, "", "get", app.tokens, function (res) {
      console.log(res)
      if ((res.data.result[0]).constructor === Array) {
        console.log("kongdsadasdsa")
      }else{
        console.log(res.data.result[0])
        wx.setStorageSync("users",res.data.result[0])
        that.setData({
          user:res.data.result[0]
        })
      }
    }, function (res) {
      wx.showToast({
        title: "内部错误",
        icon: 'none',
        duration: 1000
      })
    })
  }
  
}